﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.WPF.Shared;
using Miris.HumanMilkAnalyser.WPF.ViewModel;

namespace Miris.HumanMilkAnalyser.WPF
{
    /// <summary>
    /// Interaction logic for PatientDataSearchPage.xaml
    /// </summary>
    public partial class PatientDataSearchPage : PageBase
    {
        public PatientDataSearchPage()
        {
            InitializeComponent();
            InitializePageUnloadStoryboard();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            RegisterDeleteConfirmMessage();
            RegisterShowAnalysisResultReportMessage();
        }

        /// <summary>
        /// Register the DeleteDataConfirmMessage from PatientDataSearchViewModel.
        /// </summary>
        private void RegisterDeleteConfirmMessage()
        {
            Messenger.Default.Register<DialogMessage>(this, PatientDataSearchViewModel.DeleteDataConfirmMessageToken, dialog =>
            {
                var confirmResult = MessageBox.Show(dialog.Content, dialog.Caption, dialog.Button, dialog.Icon);
                dialog.ProcessCallback(confirmResult);
            });
        }

        /// <summary>
        /// Register the ShowAnalysisResultReportMessage from AllTestItemsViewModel.
        /// </summary>
        private void RegisterShowAnalysisResultReportMessage()
        {
            Messenger.Default.Register<AllTestItemsViewModel>(this, AllTestItemsViewModel.ShowAnalysisResultReportMessageToken, p =>
            {
                try
                {
                    var reportWindow = new AnalysisReportWindow();
                    var analysisReportViewModel = reportWindow.DataContext as AnalysisReportViewModel;
                    analysisReportViewModel.IncludeBreastfeedingSignificance = p.IncludeBreastfeedingSignificance;
                    analysisReportViewModel.SetPatientProperties(((PatientDataSearchViewModel)DataContext).SelectedPatient);
                    analysisReportViewModel.SetAllTestItemResult(p.CurrentAnalysisResult);
                    analysisReportViewModel.DignosticMessageList = new ObservableCollection<DiagnosticMessageReference>(p.DiagnosticMessageList);
                    analysisReportViewModel.AttachedDiagnosticMessage = p.CurrentAnalysisResult.AttachedDiagnosticMessage;
                    analysisReportViewModel.ResetProperties();

                    reportWindow.ShowDialog();
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }
    }
}
